﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


using UCore;

namespace Twin {
    public partial class frmWrElem : Form {
        //public frmWrElem() {
        //    InitializeComponent();
        //}

        //private void frmWrElem_Load(object sender, EventArgs e) {
        //
        //}




        public string GeneratedString { get; private set; }
        private string[] arg;
        private string args;

        
        private int INST_BITS = 0;
        private int INST_OP = 1;        //OP_WR_ELEM, [cmfg], [i_elem], [dt], [val]
        private int INST_CMFG = 2;
        private int INST_I_ELEM = 3;
        private int INST_DT = 4;
        private int INST_VAL = 5;

        private XPort xport = new XPort();

        public frmWrElem(XPort xport, string content) {           
            InitializeComponent();

            GeneratedString = content;
            this.xport = xport;
            
            arg = content.Split(',');
            
            arg[INST_CMFG] = UI.init_cmbCmfg(cmbCmfg, arg[INST_CMFG]);
            //UI.init_cmbCom(cmbCom, Ini.defaultPort);
            

            string sI_ELEM = Common.RemoveBracketedContent(arg[INST_I_ELEM]);
            string sDT = Common.RemoveBracketedContent(arg[INST_DT]);
            string sVAL = Common.RemoveBracketedContent(arg[INST_VAL]);

            if (sI_ELEM.Equals("") || sDT.Equals("") || sVAL.Equals("")) return;
            
            UI.init_listEls(listEls);
            
            //inst[INST_AREA_SN] = (Twin.TConsole.SnFile++).ToString("x");
        }


        private void frmWrElem_Load(object sender, EventArgs e) {
            this.TopMost = true;
            this.FormBorderStyle = FormBorderStyle.FixedSingle; // 设置窗体边框样式为固定单线框
            this.MaximizeBox = false;   // 移除最大化按钮
            this.MinimizeBox = false;   // 禁止调整大小

            TConsole.PositionFormAtTopRight(this);

            // 设置控件显示自定义格式的日期和时间
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            dateTimePicker1.ShowUpDown = true;
        }


        private void cmbCmfg_SelectedIndexChanged(object sender, EventArgs e) {
            arg[INST_CMFG] = cmbCmfg.SelectedIndex.ToString();
            if (arg[INST_CMFG].Equals("空")) {
                arg[INST_CMFG] = "";
            }
        }


        private void cmbCmfg_SelectedIndexChanged_1(object sender, EventArgs e) {
            arg[INST_CMFG] = cmbCmfg.SelectedItem.ToString();
            if (arg[INST_CMFG].Equals("空")) {
                arg[INST_CMFG] = "";
            }
        }


        private void btnSend_Click_1(object sender, EventArgs e) {
            arg[INST_DT] = Common.hexYYMMDDHHmmSS(dateTimePicker1.Value);
            args = arg[INST_BITS] + "," + arg[INST_OP] + "," + arg[INST_CMFG] + "," + arg[INST_I_ELEM] + "," + arg[INST_DT] + "," + arg[INST_VAL];

            //int iPort = int.Parse(cmbCom.Text.Replace("com", "")) - 1;
            CInfo cInfo = new CInfo();

            cInfo.sOpArgs = args;
            string wr_kvs = "";
            TConsole.sendAsProtocol(xport, ref cInfo, wr_kvs, "cobra");
        }

        private void btnSend_Click(object sender, EventArgs e) {

        }
    }
}
